package leetcode.秋招.网易互娱;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/9/17 19:18
 * @description：
 */
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        in.nextLine();
        HashMap<String, User> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            String s = in.nextLine();
            String[] next = s.split(" ");
            User user = new User(next[0], next[1]);
            map.put(user.user_id, user);
        }
        for (int i = 0; i < m; i++) {
            String s = in.nextLine();
            String[] next = s.split(" ");
            User user = map.get(next[0]);
            user.activitys.add(next[1]);
            user.total += Integer.parseInt(next[2]);
        }
        ArrayList<User> users = new ArrayList<>();
        for(Map.Entry<String, User> entry : map.entrySet()) {
            User user = entry.getValue();
            if (user.activitys.size() >= 2) {
                users.add(user);
            }
        }
        users.sort((o1, o2) -> {
            if (o1.total == o2.total) {
                return o1.username.compareTo(o2.username);
            } else {
                return (int) (o2.total - o1.total);
            }
        });
        for (User user : users) {
            System.out.println(user.username + " " + user.total);
        }
    }

    static class User {
        public String user_id;
        public String username;
        public HashSet<String> activitys;
        public long total;

        public User(String user_id, String username) {
            this.user_id = user_id;
            this.username = username;
            activitys = new HashSet<>();
            total = 0L;
        }
    }
}
